<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Qrset extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('qrcode_mdl');
    }

    public function index(){
        $url = '';
        $this->db->start_cache();
        $this->db->where('user_id',$_SESSION['user']['id']);
        $this->db->stop_cache();

        if(!empty($url)){
            $url = '?'.substr($url, 0 , -1);
        }

        $this->load->library('pagination');
        $config['base_url'] = site_url('qrset/index');
        $config['total_rows'] = $this->qrcode_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('id desc');
        $list = $this->qrcode_mdl->select();//echo $this->db->last_query();
        $this->db->flush_cache();

        $data['list'] = $list;
        $this->_view('index',$data);
    }

    public function create(){
        $this->_view('create');
    }

    public function update(){
        if($this->input->is_ajax_request()){
            $post = $this->input->post();
            if(empty($post['cid'])){
                die('参数错误！');
            }

            if(empty($post['name'])){
                die('二维码名称不能为空');
            }

            if(!preg_match("/^(http|https):\/\/[^\s]*$/",$post['content'])){
                die('链接格式错误，链接的格式为 ‘http://www.baidu.com’ 或者 ‘https://www.baidu.com’');
            }

            $data = array(
                'name' => $post['name'],
                'content' => $post['content']
            );

            $this->db->where('id',$post['cid']);
            $this->db->where('user_id',$_SESSION['user']['id']);
            $res = $this->qrcode_mdl->update($data);

            echo $res == true ? 1 : '修改失败！';

        }else{
            $cid = $this->input->get('cid');

            $this->db->where('id',$cid);
            $this->db->where('user_id',$_SESSION['user']['id']);

            $res = $this->qrcode_mdl->get_one();

            if(empty($res)){
                show_error('该二维码不存在！');
            }

            $this->_view('update',array('res'=>$res));
        }
    }

    public function qr_add(){
        if(!$this->input->is_ajax_request()){
            show_error('请求方式错误！');
        }
        $post = $this->input->post();

        if(empty($post['name'])){
            die('二维码名称不能为空');
        }

        if(!preg_match("/^(http|https):\/\/[^\s]*$/",$post['content'])){
            die('链接格式错误，链接的格式为 ‘http://www.baidu.com’ 或者 ‘https://www.baidu.com’');
        }

        $data = array(
            'code_id' => md5($_SERVER['HTTP_COOKIE'].$_SERVER['REQUEST_TIME_FLOAT'].rand(1,9999)),
            'content' => $post['content'],
            'create_time' => $_SERVER['REQUEST_TIME'],
            'num' => 0,
            'user_id' => $_SESSION['user']['id'],
            'name' => $post['name']
        );


        $this->db->where('user_id',$data['user_id']);
        $this->db->where('content',$data['content']);
        $res = $this->qrcode_mdl->get_one();

        if(!empty($res)){
            die('该活码已经存在');
        }

        $this->db->where('code_id',$data['code_id']);
        $res = $this->qrcode_mdl->get_one();
        if(!empty($res)){
            $data['code_id'] = md5($_SERVER['REQUEST_TIME_FLOAT'].$_SERVER['HTTP_COOKIE'].rand(1,9999));
        }
        //发送post链接， 创建图片

        $post_data = array(
            'url' => site_url('qrcode/index?code_id='.$data['code_id']),
            'name' => $data['code_id']
        );
        $r = $this->send_post(base_url('qr/data/jsqr.php'), $post_data);
        if($r == 1){
            $new_id = $this->qrcode_mdl->create($data);
            echo $new_id === false? '创建二维码失败' : $new_id;
        }else{
            die('创建二维码失败');
        }
    }

    public function show(){
        $code_id = $this->input->get('code_id');
        $this->db->where('code_id',$data['code_id']);
        $res = $this->qrcode_mdl->get_one();
        if(!empty($res)){
            $url = base_url('/qr/images/'.$res['code_id'].'.png');
            $result = get_headers($url);
            if($result[0] == 'HTTP/1.1 404 Not Found'){
                $post_data = array(
                    'url' => site_url('qrcode/index?code_id='.$res['code_id']),
                    'name' => $res['code_id']
                );
                $this->send_post(base_url('qr/data/jsqr.php'), $post_data);
            }
            die(base_url('/qr/images/'.$res['code_id'].'.png'));
        }else{
            echo '该二维码不存在！';
        }
    }

    public function delete(){
        if(!$this->input->is_ajax_request()){
            show_error('请求方式错误！');
        }
        $cid = $this->input->post('cid');
        $this->load->model('qrlog_mdl');

        //查询
        $this->db->where('id',$cid);
        $r = $this->qrcode_mdl->get_one();

        //删除文件
        $this->load->helper('file');
        if(file_exists('./qr/images/'.$r['code_id'].'.png')){
            unlink('./qr/images/'.$r['code_id'].'.png');
        }

        $this->db->where('id',$cid);
        $this->qrcode_mdl->delete();
        $this->db->where('qr_id',$cid);
        $res = $this->qrlog_mdl->delete();

        echo $res == true ? 1 : '删除失败！';
    }

}

/* End of file Qrset.php */
/* Location: ./application/controllers/Qrset.php */